package com.getpebble.android.onboarding.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.main.sections.support.fragment.NotificationDemoFragment;
import com.getpebble.android.notifications.util.CoreNotifications;
import com.getpebble.android.notifications.util.NotificationUtil;
import com.getpebble.android.onboarding.activity.OnboardingActivity;

public class EnableNotificationsFragment extends PblBaseFragment {
    public static final String TAG = EnableNotificationsFragment.class.getSimpleName();
    private Button mEnableButton;
    private Button mNotNowButton;

    public int getLayoutId() {
        return R.layout.fragment_enable_notifications;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mEnableButton = (Button) container.findViewById(R.id.onboarding_enable_btn);
        this.mNotNowButton = (Button) container.findViewById(R.id.onboarding_not_now_btn);
        this.mEnableButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("enable", "OnboardingMigration");
                EnableNotificationsFragment.this.setupNotificationListener();
            }
        });
        this.mNotNowButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("notNow", "OnboardingMigration");
                EnableNotificationsFragment.this.goToNextFragment();
            }
        });
        ((TextView) container.findViewById(R.id.onboarding_tap_enable_text)).setText(Html.fromHtml(getString(R.string.onb_s4_str_txtf_midp)));
    }

    public void onResume() {
        super.onResume();
        if (NotificationUtil.isServiceEnabled(getActivity())) {
            goToNextFragment();
        }
    }

    private void setupNotificationListener() {
        Activity activity = getActivity();
        if (activity != null) {
            NotificationUtil.showEnableServiceActivity(activity);
        }
    }

    private void goToNextFragment() {
        Activity activity = getActivity();
        if (activity != null && (activity instanceof OnboardingActivity)) {
            Fragment nextFragment;
            OnboardingActivity onboardingActivity = (OnboardingActivity) activity;
            if (CoreNotifications.allowGmailImapNotifications()) {
                nextFragment = new SetupGmailAccountsFragment();
            } else {
                Bundle args = new Bundle();
                args.putBoolean("extra_is_onboarding", true);
                nextFragment = new NotificationDemoFragment();
                nextFragment.setArguments(args);
            }
            onboardingActivity.switchScreenFragment(nextFragment);
        }
    }
}
